package com.xps.and.yuntong.Ui;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.baidu.location.BDLocation;
import com.baidu.location.BDLocationListener;
import com.baidu.location.LocationClient;
import com.baidu.location.LocationClientOption;
import com.baidu.mapapi.map.BaiduMap;
import com.baidu.mapapi.map.BitmapDescriptor;
import com.baidu.mapapi.map.BitmapDescriptorFactory;
import com.baidu.mapapi.map.MapStatus;
import com.baidu.mapapi.map.MapStatusUpdate;
import com.baidu.mapapi.map.MapStatusUpdateFactory;
import com.baidu.mapapi.map.MapView;
import com.baidu.mapapi.map.Marker;
import com.baidu.mapapi.map.MarkerOptions;
import com.baidu.mapapi.map.MyLocationConfiguration;
import com.baidu.mapapi.map.Overlay;
import com.baidu.mapapi.map.OverlayOptions;
import com.baidu.mapapi.map.Polyline;
import com.baidu.mapapi.map.PolylineOptions;
import com.baidu.mapapi.model.LatLng;
import com.jude.utils.JUtils;
import com.xps.and.yuntong.App;
import com.xps.and.yuntong.Data.bean1.OrderDetail;
import com.xps.and.yuntong.Data.model.MyTraceInfo;
import com.xps.and.yuntong.Data.net.UserNetWorks;
import com.xps.and.yuntong.R;
import com.xps.and.yuntong.Utils.GlobalPositionInfo;
import com.xps.and.yuntong.base.BaseActivity;

import java.util.ArrayList;
import java.util.List;

import rx.Subscriber;

/**
 * Created by administrator on 2017-12-15.
 */

public class DingdanWanchengActivity extends BaseActivity implements View.OnClickListener, BDLocationListener {

    private TextView tv_middle, tv_dizhi1, tv_dizhi2, tv2;
    private RelativeLayout rl_shoufei, rl_kefu, rl_pingjia, rl_all;
    private ImageView iv_fanhui, iv_right;

    /**
     * 存放所有marker
     */
    LatLng currentLatlng = null;
    private View view;
    private BaiduMap baiduMap;
    private MapView mapView;
    private LocationClient mLocationClient;
    private ArrayList<Overlay> overlays = new ArrayList<>();
    private DingdanWanchengActivity dingdanWanchengActivity;

    int span = 0;
    int locaTime = 0;
    MyTraceInfo myTraceInfo;

    //接口数据
    private String driver_order_id;

    @Override
    protected int getLayout() {
        return R.layout.activity_dingdan_wancheng;
    }

    @Override
    protected void setData() {
        orderDetail();
    }

    @Override
    protected void init(Bundle inBundle) {
        initView();
        driver_order_id = getIntent().getStringExtra("driver_order_id");
    }


    private void initView() {
        dingdanWanchengActivity = new DingdanWanchengActivity();
        mapView = (MapView) findViewById(R.id.mapView);
        tv_middle = (TextView) findViewById(R.id.tv_middle);
        tv_middle.setText(R.string.dingdan_xiangqing);
        tv_dizhi1 = (TextView) findViewById(R.id.tv_dizhi1);
        tv_dizhi2 = (TextView) findViewById(R.id.tv_dizhi2);
        tv2 = (TextView) findViewById(R.id.tv2);
        rl_shoufei = (RelativeLayout) findViewById(R.id.rl_shoufei);
        rl_kefu = (RelativeLayout) findViewById(R.id.rl_kefu);
        rl_all = (RelativeLayout) findViewById(R.id.rl_all);
        iv_fanhui = (ImageView) findViewById(R.id.iv_fanhui);
        iv_right = (ImageView) findViewById(R.id.iv_right);
        iv_right.setVisibility(View.GONE);


        rl_pingjia = (RelativeLayout) findViewById(R.id.rl_pingjia);
        setUpMap();

        rl_shoufei.setOnClickListener(this);
        rl_kefu.setOnClickListener(this);
        rl_pingjia.setOnClickListener(this);
        rl_all.setOnClickListener(this);
        iv_fanhui.setOnClickListener(this);

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.rl_shoufei:

                Intent intent = new Intent(DingdanWanchengActivity.this, DingdanJiesuanActivity.class);
                intent.putExtra("order_tiaozhuan", "1");
                intent.putExtra("driver_order_id", driver_order_id);
                startActivity(intent);
                finish();


                break;
            case R.id.rl_kefu:
                Intent phoneIntent = new Intent("android.intent.action.CALL",
                        Uri.parse("tel: 15110200250"));
                startActivity(phoneIntent);
                break;
            case R.id.rl_pingjia:
                Intent intent1 = new Intent(DingdanWanchengActivity.this, PingjiaActivity.class);
                startActivity(intent1);
                finish();
                break;

            case R.id.iv_fanhui:
                finish();
                break;
        }

    }


    void setUpMap() {
        mapView.showZoomControls(false);
        mapView.showScaleControl(false);
        baiduMap = mapView.getMap();

        baiduMap.getUiSettings().setCompassEnabled(false);
        baiduMap.getUiSettings().setZoomGesturesEnabled(true);
        baiduMap.getUiSettings().setOverlookingGesturesEnabled(false);
        baiduMap.setMyLocationEnabled(true);
        baiduMap.setViewPadding(35, 0, 20, 50);
        baiduMap.setMyLocationConfigeration(new MyLocationConfiguration(MyLocationConfiguration.LocationMode.FOLLOWING, true, null));

        mLocationClient = new LocationClient(App.getInstance());
        //activity里是否不能监听定位
        mLocationClient.registerLocationListener(this);
        periodLocation();
        huoquJingweidu();
    }

    //获取经纬度
    private void huoquJingweidu() {
        GlobalPositionInfo globalPositionInfo = App.getInstance().getGloPosinfo();
        if (globalPositionInfo == null) return;
        BDLocation bdloca = globalPositionInfo.bdLocation;
        if (bdloca == null) return;
//        if(baiduMap==null){return;}
        Log.e("111", bdloca.getLatitude() + "====" + bdloca.getLongitude());
        markerXian(bdloca);
    }

    private void markerXian(BDLocation bdloca) {


        LatLng location_point = new LatLng(bdloca.getLatitude(), bdloca.getLongitude());
        LatLng location_point1 = new LatLng(39.102798, 117.246123);
        LatLng location_point4 = new LatLng(38.103258, 117.246116);
//        dingdanWanchengActivity.addMarker(location_point,bdloca.getAddress().address,baiduMap);
        dingdanWanchengActivity.addMarker(location_point1, bdloca.getAddress().address, baiduMap);
        dingdanWanchengActivity.addMarker(location_point4, bdloca.getAddress().address, baiduMap);

        //构建折线点坐标
//        LatLng p1 = new LatLng(39.97923, 116.357428);
//        LatLng p2 = new LatLng(39.94923, 116.397428);
//        LatLng p3 = new LatLng(39.97923, 116.437428);
        List<LatLng> points = new ArrayList<LatLng>();
        points.add(location_point1);
        points.add(location_point4);
//        points.add(location_point);

        //绘制折线
        OverlayOptions ooPolyline = new PolylineOptions().width(10)
                .color(0xAAFF0000).points(points);
        Polyline mPolyline = (Polyline) baiduMap.addOverlay(ooPolyline);

    }

    /**
     * 添加一个marker 在map上
     */
    public void addMarker(LatLng lat, String str, BaiduMap baiduMap) {
        Marker marker;
        LatLng point = lat;
        BitmapDescriptor bitmap = BitmapDescriptorFactory
                .fromResource(R.mipmap.icon_markx);

        OverlayOptions option = new MarkerOptions()
                .position(point)
                .title(str)
                .icon(bitmap);
        overlays.add(baiduMap.addOverlay(option));
    }

    //周期性定位的配置
    public void periodLocation() {
        LocationClientOption option = new LocationClientOption();
        option.setLocationMode(LocationClientOption.LocationMode.Hight_Accuracy);
        option.setCoorType("bd09ll");
        int span = 1000 * 5;
        option.setScanSpan(span);
        option.setIsNeedAddress(true);
        option.setOpenGps(true);
        option.setLocationNotify(false);
        option.setIsNeedLocationDescribe(true);
        option.setIsNeedLocationPoiList(true);
        option.setIgnoreKillProcess(false);
        option.SetIgnoreCacheException(false);
        option.setEnableSimulateGps(false);
        mLocationClient.setLocOption(option);
        mLocationClient.start();
    }

    @Override
    public void onPause() {
        super.onPause();
        mapView.onPause();
    }

    @Override
    public void onResume() {
        super.onResume();
        mapView.onResume();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        onMapDestory();


    }

    public void onMapDestory() {
        mapView.onDestroy();
        baiduMap.setMyLocationEnabled(false);
        mLocationClient.stop();
        mLocationClient.unRegisterLocationListener(this);
//        geoCoder.destroy();
//        mRoutePlanSearch.destroy();
//        geoCoder = null;
        baiduMap = null;
        mapView = null;
        myTraceInfo = null;
//        handler = null;
        mLocationClient = null;
//        mRoutePlanSearch = null;
        Log.e("onMapDestory", "onMapDestory");
    }


    @Override
    public void onReceiveLocation(BDLocation bdLocation) {
        LatLng latLng = new LatLng(bdLocation.getLatitude(), bdLocation.getLongitude());
        currentLatlng = latLng;
        if (span == 0 && locaTime == 0) {
            currentLatlng = latLng;
            recoveryCamera();
            locaTime++;
        }
    }

    /**
     * 移动镜头到指定经纬度
     *
     * @param latLng 坐标点
     */
    public void moveCameraTo(LatLng latLng) {
        if (baiduMap == null) return;
        MapStatus mMapStatus = new MapStatus.Builder().target(latLng
        ).zoom(17).build();
        MapStatusUpdate u = MapStatusUpdateFactory.newMapStatus(mMapStatus);
        baiduMap.animateMapStatus(u);
    }

    public void recoveryCamera() {
        if (currentLatlng != null)
            moveCameraTo(currentLatlng);
    }


    private void orderDetail() {
        UserNetWorks.orderDetail(driver_order_id, new Subscriber<OrderDetail>() {
            @Override
            public void onCompleted() {

            }

            @Override
            public void onError(Throwable e) {

                JUtils.Toast("网络错误");
            }

            @Override
            public void onNext(OrderDetail orderDetail) {
                if (orderDetail.getReturn_code().equals("SUCCESS")) {

                    tv_dizhi1.setText(orderDetail.getReturn_body().getOrderData().getBegin_address());
                    tv_dizhi2.setText(orderDetail.getReturn_body().getOrderData().getEnd_address());
                    tv2.setText(orderDetail.getReturn_body().getOrderData().getTotal_money());


                } else if (orderDetail.getReturn_code().equals("FAIL")) {

                }

            }
        });


    }


}
